from django.conf import settings
from django.db import models
from django.contrib.auth.models import AbstractUser


# from django.contrib.auth import get_user_model
# 在别的地方引用用户时,用到下面的方法
# User = get_user_model()

# Create your models here.

class UserInfo(AbstractUser):
    '''
    用户信息
    '''
    nid = models.AutoField(primary_key=True)
    telephone = models.CharField(max_length=11, null=True, unique=True)
    #文件上传时默认下载到根目录中avatars文件夹中（如果没有Avatar文件夹，django会自动创建），存储的值为文件相对路径
    #Django对静态文件的区分 /static/ js css image  服务器自身拥有的，/media/ 用户上传的文件
    #如果配置了media_root路径，文件会下载到media_root 的avatar中
    avatar = models.FileField(upload_to='avatars/',
                              default='avatars/default.png')
    create_time = models.DateTimeField(verbose_name='创建时间',
                                       auto_now_add=True)
    blog = models.OneToOneField(to='Blog', to_field='nid', null=True,
                                on_delete=models.SET_NULL, blank=True)

    def __str__(self):
        return self.username


class Blog(models.Model):
    '''
    博客信息表(站点b)
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, verbose_name='个人博客标题')
    site_name = models.CharField(verbose_name='站点名称', max_length=64)
    # SlugFieldslug是一个新闻行业的术语。一个slug就是一个某种东西的简短标签，包含字母、数字、下划线或者连接线，
    # 通常用于URLs中。可以设置max_length参数，默认为50
    # site_name = models.SlugField(verbose_name='站点名称',max_length=50,unique=True)
    theme = models.CharField(verbose_name='博客主题', max_length=32)

    def __str__(self):
        return self.title


class Category(models.Model):
    '''
    博主个人文章分类
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32, verbose_name='分类标题')
    blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',
                             on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return self.title


class Tag(models.Model):
    '''
    博客标签
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name='标签名称', max_length=32)
    blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',
                             on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return self.title


class Article(models.Model):
    '''
    文章详情表
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name='文章标题', max_length=50)
    desc = models.CharField(verbose_name='文章简介', max_length=255, null=True)
    create_time = models.DateTimeField(verbose_name='创建时间',
                                       auto_now_add=True)
    comment_count = models.IntegerField(default=0)
    up_count = models.IntegerField(default=0)
    down_count = models.IntegerField(default=0)

    user = models.ForeignKey(verbose_name='作者', to=settings.AUTH_USER_MODEL,
                             to_field='nid', null=True, blank=True, on_delete=models.SET_NULL)
    category = models.ForeignKey(verbose_name='文章分类', to='Category',
                                 to_field='nid', null=True, blank=True, on_delete=models.SET_NULL)
    tags = models.ManyToManyField(verbose_name='文章标签', to='Tag',
                                  through='Article2Tag',
                                  through_fields=('article', 'tag'))
    content = models.TextField()

    def __str__(self):
        return self.title

    class Meta:
        ordering=['-create_time']
        verbose_name='Article'
        verbose_name_plural = 'Articles'


class Article2Tag(models.Model):
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(verbose_name='文章', to='Article', to_field='nid', on_delete=models.CASCADE)
    tag = models.ForeignKey(verbose_name='标签', to='Tag', to_field='nid', on_delete=models.CASCADE)

    class Meta:
        unique_together = [
            ('article', 'tag')
        ]

    def __str__(self):
        v = self.article.title + '---' + self.tag.title
        return v


class ArticleUpDown(models.Model):
    '''
    点赞表
    '''
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(to=settings.AUTH_USER_MODEL, null=True, blank=True,
                             on_delete=models.SET_NULL, to_field='nid')
    article = models.ForeignKey(to='Article', null=True, blank=True,
                                on_delete=models.SET_NULL, to_field='nid')
    is_up = models.BooleanField(default=True)

    class Meta:
        unique_together = [
            ('article', 'user')
        ]


class Comment(models.Model):
    '''
    评论表
    '''
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(to='Article', null=True, blank=True,
                                on_delete=models.SET_NULL, to_field='nid')
    user = models.ForeignKey(to=settings.AUTH_USER_MODEL, null=True, blank=True,
                             on_delete=models.SET_NULL, to_field='nid')
    content = models.CharField(verbose_name='评论', max_length=255, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    parent_comment = models.ForeignKey(to='self', to_field='nid', null=True, blank=True,
                                       on_delete=models.SET_NULL)

    def __str__(self):
        return self.content.ljust(30)
